/**   
 * @Title: Database.java 
 * @Package com.cdtv.util.common 
 * @Description: 
 * @author zhuw
 * @date 2012-7-6 下午4:54:05 
 */
package com.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.SharedPreferences;

import com.android.ocean.util.common.StringUtil;
import com.app.DemoApplication;
import com.modle.SkillStruct;

/**
 * 
 * @ClassName:SpJobSubscriptionUtil
 * @Description:TODO本地保存订阅的分类
 * @author zhuw
 * @date 2013-4-28 下午4:27:32
 */
public class SpJobSubscriptionUtil {

	public static String USER_SUBSCRIPTOIN_CAT = "USER_SUBSCRIPTOIN_CAT";

	/**
	 * 
	 * @Description: 字符串表现形式逗号“||”分隔： keyIndex@name;;keyIndex@name
	 * @param vs
	 * @return
	 */
	public static boolean saveSubCat(SkillStruct vs) {
		boolean flag = false;
		try {
			// 检查是否有重复，重复的数据只保存一次
			List<SkillStruct> list = getSubCatList();
			for (int i = 0; i < list.size(); i++) {
				if (vs.keyIndex.equalsIgnoreCase(list.get(i).keyIndex)) {
					list.remove(i);
					break;
				}
			}
			list.add(0, vs);
			saveSubList(list);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public static void deleteSub(SkillStruct vs) {
		List<SkillStruct> watchList = getSubCatList();
		for (int i = 0; i < watchList.size(); i++) {
			if (vs.keyIndex.equalsIgnoreCase(watchList.get(i).keyIndex)) {
				watchList.remove(i);
				break;
			}
		}
		saveSubList(watchList);

	};

	public static void clearSubList() {
		SharedPreferences.Editor editor = DemoApplication.mContext
				.getSharedPreferences(USER_SUBSCRIPTOIN_CAT, 0).edit();
		editor.clear();
		editor.commit();
		refreshMap();

	};

	private static void saveSubList(List<SkillStruct> list) {
		SharedPreferences.Editor loginEditor = DemoApplication.mContext
				.getSharedPreferences(USER_SUBSCRIPTOIN_CAT, 0).edit();
		StringBuffer sbList = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			SkillStruct vs = list.get(i);
			sbList.append(sureNoNull(vs.keyIndex)).append("@")
					.append(sureNoNull(vs.name)).append(";;");
		}
		loginEditor.putString("subscr_cat", sbList.toString());
		loginEditor.commit();
		refreshMap();
	}

	/**
	 * 
	 * @Description:TODO保证数据存入数据不会是空，否则string split方法会引起潜在错误，
	 * @param str
	 * @return
	 * @return String
	 * @author zhuw
	 * @date 2013-4-28 下午4:50:19
	 */
	private static String sureNoNull(String str) {
		if (StringUtil.isNull(str)) {
			return " ";
		}
		return str;
	}

	public static ArrayList<SkillStruct> getSubCatList() {
		ArrayList<SkillStruct> list = new ArrayList<SkillStruct>();
		SharedPreferences spold = DemoApplication.mContext
				.getSharedPreferences(USER_SUBSCRIPTOIN_CAT, 0);
		String listStr = spold.getString("subscr_cat", "");
		if (!StringUtil.isNull(listStr)) {

			String listArray[] = listStr.split(";;");
			for (int i = 0; i < listArray.length; i++) {
				SkillStruct vs = new SkillStruct();
				String[] vsStrArray = listArray[i].split("@");
				vs.keyIndex = vsStrArray[0];
				vs.name = vsStrArray[1];
				list.add(vs);
			}
		}
		return list;
	}

	public static HashMap<String, SkillStruct> localSubMap = getSubCatMap();

	private static void refreshMap() {
		localSubMap = getSubCatMap();

	}

	public static HashMap<String, SkillStruct> getSubCatMap() {
		HashMap<String, SkillStruct> map = new HashMap<String, SkillStruct>();
		SharedPreferences spold = DemoApplication.mContext
				.getSharedPreferences(USER_SUBSCRIPTOIN_CAT, 0);
		String listStr = spold.getString("subscr_cat", "");
		if (!StringUtil.isNull(listStr)) {

			String listArray[] = listStr.split(";;");
			for (int i = 0; i < listArray.length; i++) {
				SkillStruct vs = new SkillStruct();
				String[] vsStrArray = listArray[i].split("@");
				vs.keyIndex = vsStrArray[0];
				vs.name = vsStrArray[1];
				map.put(vs.keyIndex, vs);
			}
		}
		return map;
	}

}
